package com.szholly.utils.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang.time.DateUtils;


/**
 * ���ڹ����� ע��ʹ�ñ�������ǰ����������commons-lang.jar��
 * 
 * @author taoxiaoyang 2011-4-2
 * 
 */

public class DateUtil {

	/**
	 * �Ժ���Ϊ��λ��һ��
	 */
	public static final long SECOND = 1000;

	/**
	 * �Ժ���Ϊ��λ��һ����
	 */
	public static final long MINUTE = SECOND * 60;

	/**
	 * �Ժ���Ϊ��λ��һСʱ
	 */
	public static final long HOUR = MINUTE * 60;

	/**
	 * �Ժ���Ϊ��λ��һ��
	 */
	public static final long DAY = HOUR * 24;

	/**
	 * �Ժ���Ϊ��λ��һ��
	 */
	public static final long WEEK = DAY * 7;

	/**
	 * ������������,CNWEEK[0]Ϊ������
	 */
	public static final String[] CNWEEK = { "������", "����һ", "���ڶ�", "������", "������",
			"������", "������" };

	/**
	 * Ӣ����������,CNWEEK[0]ΪSun
	 */
	public static final String[] ENWEEK = { "Sun", "Mon", "Tue", "Wed", "Thu",
			"Fri", "Sat" };

	/**
	 * Ӣ���·�����,ENMONTH[0]Ϊһ��
	 */
	public static final String[] ENMONTH = { "Jan", "Feb", "Mar", "Apr", "May",
			"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

	/**
	 * �����·�����,ENMONTH[0]Ϊ01
	 */
	public static final String[] NUMMONTH = { "01", "02", "03", "04", "05",
			"06", "07", "08", "09", "10", "11", "12" };

	/**
	 * Ĭ�ϵ����ڸ�ʽ:yyyy-MM-dd HH:mm:ss
	 */
	public static final String DEFAULTFORMAT = "yyyy-MM-dd HH:mm:ss";

	/**
	 * ���ڸ�ʽ��� 0 �� Ĭ�ϵ����ڸ�ʽyyyy-MM-dd HH:mm:ss(24Сʱ��) 1 �� yyyy-MM-dd
	 * hh:mm:ss(12Сʱ��) 2 �� yyyy-MM-dd 3 �� dd MM yyyy HH:mm:ss z 4 �� yyyy/MM/dd
	 * HH:mm:ss 5 �� yyyy/MM/dd 6 �� yyyy-MM-dd HH:mm:ss.S
	 */
	public static final String[] DATEFORMATS = { "yyyy-MM-dd HH:mm:ss",
			"yyyy-MM-dd hh:mm:ss", "yyyy-MM-dd", "dd MM yyyy HH:mm:ss z",
			"yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd", "yyyy-MM-dd HH:mm:ss.S", };

	public DateUtil() {
	}

	/**
	 * ��ݴ������ڸ�ʽ�����ص�ǰ����
	 * 
	 * @param format
	 *            ���ڸ�ʽ,���ʽ���ڲ�����,����Ĭ�ϸ�ʽyyyy-MM-dd hh:mm:ss
	 * @return String ��ʽ����ǰ����
	 */
	public static synchronized String getCurrentDateForFormat(String format) {
		Calendar c = Calendar.getInstance();
		try {
			DateFormat df = new SimpleDateFormat(format);
			return df.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
			return new SimpleDateFormat(DATEFORMATS[0]).format(c.getTime());
		}
	}

	/**
	 * ��ݴ�������ں����ڸ�ʽ����ʽ������
	 * 
	 * @param date
	 *            Ҫ��ʽ��������
	 * @param format
	 *            ���ڸ�ʽ,���ʽ���ڲ�����,����Ĭ�ϸ�ʽyyyy-MM-dd HH:mm:ss
	 * @return String �Ը�ʽ������
	 */
	public static synchronized String getDateForFormat(Date date, String format) {
		try {
			if (date == null)
				return "";
			DateFormat df = new SimpleDateFormat(format);
			return df.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return new SimpleDateFormat(DATEFORMATS[0]).format(date);
		}
	}

	/**
	 * ��ݴ�������ں����ڸ�ʽ����ʽ������
	 * 
	 * @param c
	 *            Ҫ��ʽ��������
	 * @param format
	 *            ���ڸ�ʽ,���ʽ���ڲ�����,����Ĭ�ϸ�ʽyyyy-MM-dd HH:mm:ss
	 * @return String �Ը�ʽ������
	 */
	public static synchronized String getDateForFormat(Calendar c, String format) {
		try {
			DateFormat df = new SimpleDateFormat(format);
			return df.format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
			return new SimpleDateFormat(DATEFORMATS[0]).format(c.getTime());
		}
	}

	/**
	 * ��ݴ�����ַ����ں������ڸ�ʽ����ʽ������
	 * 
	 * @param date
	 *            Ҫ��ʽ��������
	 * @param oldFormat
	 *            �����ַ�����ڸ�ʽ
	 * @param newFormat
	 *            Ҫת���ɵ����ڸ�ʽ
	 * @return String �Ը�ʽ������,��ʽ���쳣����null
	 */
	public static synchronized String getDateForFormat(String date,
			String oldFormat, String newFormat) {
		try {
			DateFormat oldDf = new SimpleDateFormat(oldFormat);
			DateFormat newDf = new SimpleDateFormat(newFormat);
			return newDf.format(oldDf.parse(date));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * ���ַ�ת��Ϊ��������
	 * 
	 * @param strDate
	 *            �ַ��ʽ����
	 * @return Date ��������,�쳣����null
	 */
	@SuppressWarnings("static-access")
	public static synchronized Date strToDate(String strDate) {
		try {
			if (strDate != null && !strDate.equals("")) {
				DateUtils d = new DateUtils();
				return d.parseDate(replaceEN(strDate), DATEFORMATS);
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * ���ַ�ת��Ϊ��������
	 * 
	 * @param strDate
	 *            �ַ��ʽ����
	 * @param format
	 *            ���ڸ�ʽ
	 * @return Date ��������,�쳣����null
	 */
	@SuppressWarnings("static-access")
	public static synchronized Date strToDate(String strDate, String format) {
		try {
			org.apache.commons.lang.time.DateUtils d = new DateUtils();
			return d.parseDate(replaceEN(strDate), new String[] { format });
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * ���ش������������ڣ���ǰ�������ø���
	 * 
	 * @param date
	 *            ����
	 * @param day
	 *            ����
	 * @return String ���ص����ڸ�ʽ��yyyy-MM-dd HH:mm:ss
	 */
	public static String getDateAfterDay(Date date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_MONTH, day);
		return getDateForFormat(c.getTime(), DATEFORMATS[0]);
	}

	/**
	 * ���ش������������ڣ���ǰ�������ø���
	 * 
	 * @param day
	 *            ����
	 * @return String ���ص����ڸ�ʽ��yyyy-MM-dd HH:mm:ss
	 */
	public static String getDateAfterMonth(int month) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, month);
		return getDateForFormat(c.getTime(), DATEFORMATS[0]);
	}

	/**
	 * ���ش������������ڣ���ǰ�������ø���
	 * 
	 * @param day
	 *            ����
	 * @return String ���ص����ڸ�ʽ��yyyy-MM-dd HH:mm:ss
	 */
	public static String getDateAfterYear(int year) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.YEAR, year);
		return getDateForFormat(c.getTime(), DATEFORMATS[0]);
	}

	/**
	 * ��ȡ��ǰʱ�������
	 * 
	 * @return String ���ڼ�
	 */
	public static String getCurrentCNWeek() {
		Calendar c = Calendar.getInstance();
		return CNWEEK[c.get(Calendar.DAY_OF_WEEK) - 1];
	}

	/**
	 * ���ش������������ڼ�
	 * 
	 * @param c
	 * @return String ���ڼ�
	 */
	public static String getCNWeek(Calendar c) {
		if (c == null) {
			return null;
		} else {
			return CNWEEK[c.get(Calendar.DAY_OF_WEEK) - 1];
		}
	}

	/**
	 * �滻�ַ������е�Ӣ���·ݺ�����
	 * 
	 * @param strDate
	 *            Դ�ַ�
	 * @return String �滻���ַ�
	 */
	public static String replaceEN(String strDate) {
		if (strDate != null && !strDate.equals("")) {
			// �滻Ӣ���·�
			for (int i = 0; i < ENMONTH.length; i++) {
				if (strDate.contains(ENMONTH[i]))
					strDate = strDate.replace(ENMONTH[i], NUMMONTH[i]);
			}
			// �滻Ӣ������
			for (int i = 0; i < ENWEEK.length; i++) {
				if (strDate.contains(ENWEEK[i]))
					strDate = strDate.replace(ENWEEK[i], CNWEEK[i]);
			}
			return strDate;
		} else {
			return null;
		}
	}

	/**
	 * ��ȡ��ǰ����Ϊ����ĵڼ���
	 * 
	 * @return �ڼ���
	 */
	public static int getCurrentWeek() {
		Calendar c = new GregorianCalendar();
		return c.get(Calendar.WEEK_OF_YEAR);
	}

	/**
	 * ��ȡ����һ
	 * 
	 * @return ��һ������
	 */
	public static Date getMonday() {
		Calendar calendar = new GregorianCalendar();
		// ȡ�ñ���һ
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return calendar.getTime();
	}
	
	/**
	 * ��ȡ����һ
	 * 
	 * @return ��һ������
	 */
	public static Date getMonday(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		// ȡ�ñ���һ
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return calendar.getTime();
	}

	/**
	 * ��ȡ��������
	 * 
	 * @return ��������
	 */
	public static Date getSunday() {
		Calendar calendar = new GregorianCalendar();
		// ȡ�ñ���һ
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return new Date(calendar.getTime().getTime()
				+ (6 * 24 * 60 * 60 * 1000));
	}
	
	/**
	 * ��ȡ��������
	 * 
	 * @return ��������
	 */
	public static Date getSunday(Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		// ȡ�ñ���һ
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return new Date(calendar.getTime().getTime()
				+ (6 * 24 * 60 * 60 * 1000));
	}
	
	/**
	 * ���㴫�������뵱ǰ������������
	 * @param date
	 * @return
	 */
	public static String getDiffDay(Date date){
		try {
			long DAY = 24L * 60L * 60L * 1000L;    
			return "" + (( new Date().getTime() - date.getTime() ) / DAY );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * ���㴫�������뵱ǰ������������
	 * @param date
	 * @return
	 */
	public static long getDiffDayEx(Date date){
		try {
			long DAY = 24L * 60L * 60L * 1000L;    
			return (( new Date().getTime() - date.getTime() ) / DAY );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1L;
	}
	
	/**
	 * ���㴫�������뵱ǰ��������Сʱ
	 * @param date
	 * @return
	 */
	public static long getDiffDayEx2(Date date){
		try {
			long DAY = 10L * 60L * 1000L;//账号锁定时间  (分钟*秒*毫秒)
			return (( new Date().getTime() - date.getTime() ) / DAY );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1L;
	}
	
	/**
	 * ���㴫�������뵱ǰ�������ķ���
	 * @param date
	 * @return
	 */
	public static long getDiffDayEx3(Date date){
		try {
			long DAY = 60L * 1000L;    
			return (( new Date().getTime() - date.getTime() ) / DAY );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	/**
	 * ���㴫�������뵱ǰ����������
	 * @param date
	 * @return
	 */
	public static long getDiffDayEx4(Date date){
		try {
			long DAY = 1000L;    
			return (( new Date().getTime() - date.getTime() ) / DAY );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	/**
	 * ���㴫��������������
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static String getDiffDay(Date d1,Date d2){
		try {
			long DAY = 24L * 60L * 60L * 1000L;    
			return "" + (( d1.getTime() - d2.getTime() ) / DAY );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static void main(String[] args) throws ParseException {
		System.out.println(DateUtil.getDiffDay(DateUtil.strToDate("2012-05-19"),new Date()));
	}

}
